using Microsoft.AspNetCore.Mvc;
using UniversalAdmin.Application.Dtos;
using UniversalAdmin.Application.Dtos.Role;
using UniversalAdmin.Application.Services.Interfaces;

namespace UniversalAdmin.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class RoleController : ControllerBase
{
    private readonly IRoleService _roleService;

    public RoleController(IRoleService roleService)
    {
        _roleService = roleService;
    }

    /// <summary>
    /// 获取所有角色列表（分页查询）
    /// </summary>
    /// <param name="pageRequestDto">分页查询参数</param>
    /// <returns>分页角色列表</returns>
    [HttpGet]
    public async Task<IActionResult> GetAllRoles([FromQuery] PageRequestDto pageRequestDto)
    {
        var roles = await _roleService.GetAllRolesAsync(pageRequestDto);
        return Ok(roles);
    }


    [HttpGet("{roleId}")]
    public async Task<IActionResult> GetRoleById(Guid roleId)
    {
        var role = await _roleService.GetRoleByIdAsync(roleId);
        return Ok(role);
    }

    /// <summary>
    /// 创建新角色
    /// </summary>
    /// <param name="createRoleDto">角色创建参数</param>
    /// <returns>创建结果</returns>
    [HttpPost]
    public async Task<IActionResult> CreateRole([FromBody] CreateRoleDto createRoleDto)
    {
        var result = await _roleService.CreateRoleAsync(createRoleDto);
        return Ok(result);
    }

    /// <summary>
    /// 更新角色信息
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <param name="updateRoleDto">角色更新参数</param>
    /// <returns>更新结果</returns>
    [HttpPut("{roleId}")]
    public async Task<IActionResult> UpdateRole(Guid roleId, [FromBody] UpdateRoleDto updateRoleDto)
    {
        var result = await _roleService.UpdateRoleAsync(roleId, updateRoleDto);
        return Ok(result);
    }

    /// <summary>
    /// 删除角色（软删除）
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>删除结果</returns>
    [HttpDelete("{roleId}")]
    public async Task<IActionResult> DeleteRole(Guid roleId)
    {
        var result = await _roleService.DeleteRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 启用角色
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>操作结果</returns>
    [HttpPatch("{roleId}/enable")]
    public async Task<IActionResult> EnableRole(Guid roleId)
    {
        var result = await _roleService.EnableRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 禁用角色
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>操作结果</returns>
    [HttpPatch("{roleId}/disable")]
    public async Task<IActionResult> DisableRole(Guid roleId)
    {
        var result = await _roleService.DisableRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 为角色分配菜单权限
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <param name="menuId">菜单ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("{roleId}/menus/{menuId}")]
    public async Task<IActionResult> AssignMenuToRole(Guid roleId, Guid menuId)
    {
        var result = await _roleService.AssignMenuToRoleAsync(roleId, menuId);
        return Ok(result);
    }

    /// <summary>
    /// 批量为角色分配菜单权限
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <param name="assignMenusDto">批量分配菜单权限参数</param>
    /// <returns>操作结果</returns>
    [HttpPost("{roleId}/menus")]
    public async Task<IActionResult> AssignMenusToRole(Guid roleId, [FromBody] AssignMenusToRoleDto assignMenusDto)
    {
        var result = await _roleService.AssignMenusToRoleAsync(roleId, assignMenusDto);
        return Ok(result);
    }

    /// <summary>
    /// 为角色一键分配所有菜单权限
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("{roleId}/menus/all")]
    public async Task<IActionResult> AssignAllMenusToRole(Guid roleId)
    {
        var result = await _roleService.AssignAllMenusToRoleAsync(roleId);
        return Ok(result);
    }

    /// <summary>
    /// 移除角色的菜单权限
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <param name="menuId">菜单ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{roleId}/menus/{menuId}")]
    public async Task<IActionResult> RemoveMenuFromRole(Guid roleId, Guid menuId)
    {
        var result = await _roleService.RemoveMenuFromRoleAsync(roleId, menuId);
        return Ok(result);
    }

    /// <summary>
    /// 获取角色的菜单权限
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>角色的菜单权限列表</returns>
    [HttpGet("{roleId}/menus")]
    public async Task<IActionResult> GetRoleMenus(Guid roleId)
    {
        var result = await _roleService.GetRoleMenusAsync(roleId);
        return Ok(result);
    }
}